/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.servicelayer.xmlconvertors;

import cz.fi.muni.diplomka.common.Unit;
import cz.fi.muni.diplomka.common.enumeration.ResourceType;
import cz.fi.muni.diplomka.common.enumeration.UnitType;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * Class used for getting Unit info from XML
 *
 * @author Jakub Kutil
 */
public class UnitConvertor {

    private LocationString locationString = new LocationString();
    /**
     * Gets Unit from XML
     *
     * @param unitName name of Unit to get info about
     *
     * @return unit info
     */
    public Unit getUnitFromXML(String unitName) {

        Unit unit = new Unit();

        try {

            File fXmlFile = new File(locationString.getXMLLocation() + "\\units.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);

            XPath xpath = XPathFactory.newInstance().newXPath();
            String expression = "/units/unit[@id='" + unitName + "']";
            Node node = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE);

            Element element = (Element) node;

            unit.setName(UnitType.valueOf(element.getAttribute("id")));
            unit.setArmor(Integer.parseInt(element.getElementsByTagName("armor").item(0).getTextContent()));
            unit.setAttack(Integer.parseInt(element.getElementsByTagName("attack").item(0).getTextContent()));
            unit.setCarry(Integer.parseInt(element.getElementsByTagName("carry").item(0).getTextContent()));
            unit.setDamageHigh(Integer.parseInt(element.getElementsByTagName("damageHigh").item(0).getTextContent()));
            unit.setDamageLow(Integer.parseInt(element.getElementsByTagName("damageLow").item(0).getTextContent()));
            unit.setDefense(Integer.parseInt(element.getElementsByTagName("defense").item(0).getTextContent()));
            unit.setHealth(Integer.parseInt(element.getElementsByTagName("health").item(0).getTextContent()));
            unit.setInitiative(Integer.parseInt(element.getElementsByTagName("initiative").item(0).getTextContent()));
            unit.setPriceFood(Integer.parseInt(element.getElementsByTagName("priceFood").item(0).getTextContent()));
            unit.setPriceGold(Integer.parseInt(element.getElementsByTagName("priceGold").item(0).getTextContent()));
            unit.setSpeed(Integer.parseInt(element.getElementsByTagName("speed").item(0).getTextContent()));
            unit.setLevel(Integer.parseInt(element.getElementsByTagName("level").item(0).getTextContent()));

            List<ResourceType> equipment = new ArrayList();
            for (int i = 0; i < element.getElementsByTagName("equipment").getLength(); i++) {

                ResourceType e = ResourceType.valueOf(element.getElementsByTagName("equipment").item(i).getTextContent());
                equipment.add(e);
            }

            unit.setEquipment(equipment);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return unit;
    }
}
